package fun.coding.leetcode;

public class BestTimeToBuyAndSellStockII {

	public static void main(String[] args) {

	}
	
	//  this question turns to sum all the positive price difference.
	// Let's say we have 2, 5, 7, |5-2|, |7-5| , since we can sell and buy at the same point.
	// 2 1 9 -> 8 instead of 7
	public int maxProfit(int[] prices) {
		if (prices == null || prices.length <= 1) return 0;
		
		int maxProfit = 0;
		int last = prices[0];
		
		for (int i = 0; i < prices.length; i++) {
			if (prices[i] > last) {
				maxProfit += prices[i] - last;
			}
			last = prices[i];
		}
		return maxProfit;
	}
	/* easier implementation, only i+1 - i is enough.
	public class Solution {
	    public int maxProfit(int[] prices) {
	        int profit = 0;
	        for (int i = 0; i < prices.length - 1; i++) {
	            int diff = prices[i+1] - prices[i];
	            if (diff > 0) {
	                profit += diff;
	            }
	        }
	        return profit;
	    }
	}
	*/
}
